//
//  AppCustom.m
//  SBooks
//
//  Created by shansander on 2016/11/12.
//  Copyright © 2016年 shansander. All rights reserved.
//

#import "AppCustom.h"
#import <NetworkExtension/NetworkExtension.h>


@implementation AppCustom
//暂时不实用这个
//http://www.jianshu.com/p/15e90bf11c51
+ (NSString *)getWiFIName
{
    NSArray * networkInterfaces = [NEHotspotHelper supportedNetworkInterfaces];
    
    NSLog(@"Network %@",networkInterfaces);
    
    for (NEHotspotNetwork * hotsportNetwork in networkInterfaces) {
        NSString * ssid = hotsportNetwork.SSID;
        NSLog(@"ssid : %@",ssid);
    }
    
    return @"";
    
}

+ (NSString *)getSavedFileDirctionPath
{
    NSArray * dirctionPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsPath = [dirctionPath firstObject];
    NSString * uploadPath = [documentsPath stringByAppendingPathComponent:@"upload"];
    return uploadPath;
}


+ (NSString *)sd_contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];
    switch (c) {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
        case 0x52:
            // R as RIFF for WEBP
            if ([data length] < 12) {
                return nil;
            }
            
            NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
            if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
                return @"image/webp";
            }
            
            return nil;
    }

    return nil;
}

+ (void)NSURLSessionGetMiMeTypeWithPath:(NSString * )path andResult:(Finish)finish
{
    NSURL * url = [NSURL fileURLWithPath:path];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    
    NSURLSession * session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask * dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",response.MIMEType);
        finish(response.MIMEType);
    }];
    [dataTask resume];
}

+ (NSString *)CalculatesFileSize:(NSNumber * )filesize
{
    NSInteger file_size_b = [filesize integerValue]; // b
    if (file_size_b < 1000) {
        return [NSString stringWithFormat:@"%ldb",(long)file_size_b];
    }
    CGFloat file_size_kb = file_size_b / 1024.0f; // kb
    
    if (file_size_kb < 1000) {
        return [NSString stringWithFormat:@"%0.2fkb",file_size_kb];
    }
    
    CGFloat file_size_mb = file_size_kb / 1024.0f; // mb
    
    if (file_size_mb < 1000) {
        return [NSString stringWithFormat:@"%0.2fMB",file_size_mb];
    }
    
    CGFloat file_size_G = file_size_mb / 1024.0f; //g
    if (file_size_G < 1000) {
        return [NSString stringWithFormat:@"%0.2fG",file_size_G];
    }

    return @"";
}

+ (NSString *)getCreateTimeString:(NSDate *)fileDate
{
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter stringFromDate:fileDate];
}


@end
